package lock

import (
	"runtime"
	"strings"
	"sync/atomic"
)

type spinLocker struct {
	Mutex
	key *uint32
	old uint32
}

// 如果没有获取到锁一直自旋
func (s *spinLocker) lock(builder *strings.Builder) {
	for !atomic.CompareAndSwapUint32(s.key, s.old, s.old+1) {
		builder.WriteString("自旋转中")
		runtime.Gosched()
	}
	s.Lock()
}

func (s *spinLocker) unlock() {
	s.Unlock()
	atomic.StoreUint32(s.key, s.old)
}
